1466D - 13th Labour of Heracles - CodeForces Solution


data structures greedy sortings trees *1500

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <bits/stdc++.h>
// #include "utilities.cpp"
using namespace std;
#define int long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define forn(i, x, n) for (int i = x; i < n; i++)
#define vi vector<int>
#define vpp vector<pair<int,int>>
#define vs vector<string>
#define vll vector<long long>
#define mod 1000000007; 
int row[] = {1,0,-1,0};
int col[] = {0,1,0,-1};


void solve(){

    int n;
    cin>>n;

    vpp wt(n); 
    vi inD(n+1, -1);
    int sum = 0;
    forn(i,0,n){
        cin>>wt[i].first;
        sum += wt[i].first;
        wt[i].second = i+1;
    }
    vector<int> adj[n];

    forn(i,0,n-1){

        int a, b;
        cin>>a>>b;
        inD[a]++;
        inD[b]++;
    }

    sort(rall(wt));
    int j = 0;
    cout<<sum<<' ';

    forn(i, 1, n-1){

        if(j == wt.size()){
            cout<<sum<<' ';
        }else{

            int k = wt[j].second;
            while(inD[k] <= 0){

                j++;
                if(j == wt.size()) break;
                k = wt[j].second;
            } 
            if(j == wt.size()){
                cout<<sum<<' ';
            }else{

                inD[k]--;
                sum += wt[j].first;
                cout<<sum<<' ';
            }
        }
    }
    cout<<'\n';
}


signed main(){
    int t = 1; 
    cin >> t;
    while (t--) solve();    
    return 0;
}


Comments

Submit
0 Comments
More Questions

780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack